#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std;


struct node {
	int u, v;
	int w;
	node(int ui, int vi, int wi) {
		u = ui;
		v = vi;
		w = wi;
	}
};

int S[5001];
bool USE[5001];
vector<node> E;

int getf(int x) {
	if (S[x] == x) return x;
	return S[x] = getf(S[x]);
}

void merage(int x, int y) {
	int a = getf(x), b = getf(y);
	if (a != b) {
		S[a] = b;
		S[x] = b;
	}
}

bool cmp(node x, node y) {
	return x.w < y.w;
}

int main() {
	int n, p, xi, yi, wi;
	scanf("%d %d", &n, &p);
	for (int i = 0; i <= p; ++i) {
		S[i] = i;
	}
	for (int i = 1; i <= p; ++i) {
		scanf("%d %d %d", &xi, &yi, &wi);
		E.push_back(node(xi, yi, wi));
	}
	sort(E.begin(), E.end(), cmp);
	int index = 1;
	unsigned np = 0;
	while (index <= p - 1) {
		if (np >= E.size()) {break;} 
		node now = E[np++];
		if (getf(now.u) == getf(now.v)) continue;
		++index;
		USE[np - 1] = 1;
		merage(now.u, now.v);
	}
	int nowans = 0x7fffffff;
	for (unsigned i = 0; i < E.size(); ++i) {
		if (USE[i]) continue;
		for(int j = 1; j <= n; ++j) S[j]=j;
		int ans = E[i].w;
		merage(E[i].u, E[i].v);
		index = 1;
		np = 0;
		while (index <= p - 2) {
			if (np >= E.size()) {break;} 
			node now = E[np++];
			if (getf(now.u) == getf(now.v)) continue;
			++index;
			ans += now.w;
			merage(now.u, now.v);
		}
		nowans = min(nowans, ans);
	}
	printf("%d\n", nowans);
	
	return 0;
}

